package com.atguigu.Servlet;

import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * 类说明：
 *      用户模块的Servlet程序
 * @author ChenCheng
 * @ClassName UserServlet
 * @create 2023-02-05 17:13
 * @Version 1.0
 */
//public class UserServlet extends HttpServlet {
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();
//    @Override
//    protected void doPost(
//            HttpServletRequest request,
//            HttpServletResponse response)
//            throws ServletException, IOException {
//
//        String action = request.getParameter("action");
//
//
//        // 方式一:合并LoginServlet于RegistServlet程序为 UserServlet程序
////        if("login".equals(action)){
////            // 登录
////            login(request,response);
////        }else if("register".equals(action)){
////            // 注册
////            register(request,response);
////        }
//
//
//        // 方式二:使用反射优化 if(){} else if(){}
//        try {
//            this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class)
//                    .invoke(this, request, response);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        // 方式三:抽取BaseServlet程序
          // 这样请求发给了UserServlet 会先走父类BaseServlet的doPost方法  在反射调用子类的方法
//    }


    /**
     * 方法说明：
     * @MethodName 处理登录的方法
     * @Description
     * @Author Chen
     * @EMAIL <a href="2277411582@qq.com"></a>
     * @Date 2023/2/5 17:36
     */
    protected void login(HttpServletRequest request,
                         HttpServletResponse response
    ) throws ServletException, IOException {

        //  1、获取请求的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 调用 userService.login()登录处理业务
        User loginUser = userService.login(new User(null, username, password, null));
        // 如果等于null,说明登录 失败!
        if (loginUser == null) {
            //   跳回登录页面
//            request.getRequestDispatcher("/pages/user/login.html").forward(req, resp);
            request.setAttribute("meg","用户名或密码错误");
            request.setAttribute("username",username);
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        } else {
            // 登录 成功
            //跳到成功页面login_success.html
//            request.getRequestDispatcher("/pages/user/login_success.html").forward(request, response);
            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request, response);
        }

    }

    /**
     * 方法说明：
     * @MethodName 处理注册的方法
     * @Description
     * @Author Chen
     * @EMAIL <a href="2277411582@qq.com"></a>
     * @Date 2023/2/5 17:36
     */
    protected void register(HttpServletRequest request,
                         HttpServletResponse response
    ) throws ServletException, IOException {
//  1、获取请求的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String code = request.getParameter("code");

        User user = WebUtils.copyParamToBean(request.getParameterMap(), new User());

//        2、检查 验证码是否正确  === 写死,要求验证码为:abcde
        if ("abcde".equalsIgnoreCase(code)) {
//        3、检查 用户名是否可用
            if (userService.existsUsername(username)) {
                System.out.println("用户名[" + username + "]已存在!");
//        跳回注册页面
                // 回显信息保存到Request域中
                request.setAttribute("msg", "用户名已存在！！");
                request.setAttribute("username", username);
                request.setAttribute("email", email);
                request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            } else {
                //      可用
//                调用Sservice保存到数据库
//                userService.registUser(new User(null, username, password, email));
                userService.registUser(user);
//
//        跳到注册成功页面 regist_success.html
                request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request, response);
            }
        } else {
            request.setAttribute("msg", "验证码错误！！");
            request.setAttribute("username", username);
            request.setAttribute("email", email);
            System.out.println("验证码[" + code + "]错误");
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
        }
    }
}
